<script type="text/javascript" language="javascript" charset="utf-8">
$(".menu-wrap li").removeClass("active-link");
$(".menu-wrap #student").addClass("active-link");
</script>
<div class="common-tit">
  <h2>编辑学员</h2>
  <span><%= link_to "学员系统", admin_students_path %> &gt; <%= link_to "学员详情", admin_student_path(@student) %> &gt; 编辑学员</span>
</div>
<div class="cur-box">
  <h3>
    <span>基本信息</span>
  </h3>
  <div class="cur-basic clearfix">
    <div class="ads-info clearfix">
      <div id="upload_avatar_container" class="camera">
        <input type="hidden" id="student_avatar" value="">
         <% if @student.avatar.present? %>
            <%= image_tag("#{@student.avatar_url}", size: "150x150", alt: "头像" ,id: "student_avatar_img")%>
          <% else %>
            <%= image_tag("/avatar.png", size: "150x150", alt: "头像" ,id: "student_avatar_img")%>   
          <% end %>  
        <div  id="upload_avatar" class="upl">本地上传</div>
      </div>
      <ul class="bs-box">
        <li>
          <input type="text" class="in-l required" placeholder="姓名" value="<%= @student.name %>">
          <input type="text" class="in-r" placeholder="别名 / 英文名" value="<%= @student.alias_name %>">
        </li>
        <li>
          <div class="list-box b-sex" choice="mandatory">
            <div class="downbtn">
              <span data_id="<%= @student.sex %>"><%= @student.sex %></span>
              <em></em>
            </div>
            <ul class="dropdown-menu">
              <% Company::GENDER.each do |key,value| %>
                <li data_id="<%= key %>"><%= value %></li>
              <%end%>
            </ul>
          </div>
          <b>请选择</b>
          <div class="list-box group" choice="mandatory">
            <div class="downbtn">
              <span data_id="<%= @student.student_group %>"><%= @student.student_group ? @student.student_group_configuration.group_name : "学员分组"  %></span>
              <em></em>
            </div>
            <ul class="dropdown-menu">
              <li class="cho-tit">学员分组</li>
              <% @student_group.each do|sg| %>
                <li data_id="<%= sg.id %>"><%= sg.group_name %></li>
              <% end %>
            </ul>
          </div>
        </li>
        <li>
          <input id="stuTel" type="text" class="in-l" placeholder="手机号" value="<%= @student.mobile %>" readonly="readonly">
          <input type="text" class="in-r" placeholder="微信号" value="<%= @student.weixin %>">
        </li>
        <li>
          <div class="date-ibox dr">
            <input type="text" class="in-l date-input" readOnly="true" placeholder="出生年月" value="<%= @student.birthday %>">
          </div>
          <div class="list-box group" choice="mandatory">
            <div class="downbtn">
              <span data_id="<%= @student.source_from %>"><%= @student.source_from ? @student.source_from : "信息来源"  %></span>
              <em></em>
            </div>
            <ul class="dropdown-menu">
              <li class="cho-tit">信息来源</li>
              <% Company::SOURCE_FROM.each do |key,value| %>
                <li data_id="<%= key %>"><%= value %></li>
              <% end -%>
            </ul>
          </div>
        </li>
        <li>
          <input type="text" class="in-l" placeholder="紧急联系人姓名" value="<%= @student.emergency_contact %>">
          <input type="text" class="in-r" placeholder="紧急联系人电话" value="<%= @student.emergency_mobile %>">
        </li>
        <li>
          <input type="text" class="in-a" placeholder="联系地址" value="<%= @student.address %>">
        </li>
        <li>
          <% if @student.remark.blank? %>
            <% remark = "#{@student.learning_requirement} #{@student.state_of_business} #{@student.learning_intention}" %>
            <input type="text" class="in-a" placeholder="备注信息" value="<%= remark %>">
          <%else%>
            <input type="text" class="in-a" placeholder="备注信息" value="<%= @student.remark %>">
          <% end %>
        </li>
      </ul>	
    </div>
  </div>
  <%= tag(:input, :type => 'hidden', :name => 'utf-8',:value => "✓") %>
  <%= tag(:input, :type => "hidden", :name =>  request_forgery_protection_token.to_s, :value => form_authenticity_token) %> 
  <div class="intention-btn">保存</div>
</div>
<script src="/assets/js/common.js"></script>
<script>
$(function(){
  $('.date-list1').date_input();
  $(".date-list2").date_input();
  $(".date-input").date_input();
});
$('.date-list2').on("click", function() {
  updateT1();
  $('.nav span').on("click", function() {
    if ($(this).parent().parent().parent().prev().attr("class") == "date-list2") {
      updateT1();
    };
  });
});
$('.date-list1').on("click", function() {
  updateT2();
  $('.nav span').on("click", function() {
    if ($(this).parent().parent().parent().prev().attr("class") == "date-list1") {
      updateT2();
    };
  });
});
$(".intention-btn").on("click", function() {
  if(adstuSet()) {
    var postJson = {
      "student_id" : "<%= @student.id %>",
      "student" : [{
        "name" : $(".bs-box input").eq(0).val(),
        "alias_name" : $(".bs-box input").eq(1).val(),
        "sex" : $(".b-sex").find("span").text(),
        "student_group" : $(".bs-box .group").eq(0).find("span").attr("data_id"),
        "weixin" : $(".bs-box input").eq(3).val(),
        "birthday" : $(".bs-box input").eq(4).val(),
        "source_from" : $(".bs-box .group").eq(1).find("span").attr("data_id"),
        "emergency_contact" : $(".bs-box input").eq(5).val(),
        "emergency_mobile" : $(".bs-box input").eq(6).val(),
        "address" : $(".bs-box input").eq(7).val(),
        "avatar" : $("#student_avatar").val(),
        "remark" : $(".bs-box input").eq(8).val()
      }],
      'utf-8': $("input[name=utf-8]").val(),
      'authenticity_token': $("input[name=authenticity_token]").val()
    };
    $.ajax({
      data: postJson,
      type:'post', url:"/admin/students/save_student",
      success: function(data) {
        if (data.error) {
          // console.log("wrong");
        } 
      }
    });
  }
})
$(".bs-box .list-box li").on("click", function() {
  $(this).parent().parent().find("span").attr("data_id", $(this).attr("data_id"));
})
// 验证是否必填
function adstuSet() {
  var writeIn = true;
  for (var i = 0; i < $(".bs-box .required").length; i++) {
    if ($($(".required")[i]).val() == "") {
      $($(".required")[i]).css({
        "border" : "1px solid #ff5a60" 
      });
      writeIn = false;
    } else {
      $($(".required")[i]).css({
        "border" : "1px solid #9ea0b7" 
      });
    }
  }
  return writeIn;
}
</script>
